﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;

namespace QuanLyKTX
{
    using DAL;
    using System.Linq;
    public partial class frm_EndContract : Telerik.WinControls.UI.RadForm
    {
        #region Properties
        public Guid MaHopDong { get; set; }
        #endregion
        #region Private Methods
        private void DefaultLoadForm()
        {
            radDateTimePicker_NgayThanhLy.Value = DateTime.Now;
            radDropDownList_DangThanhLy.ListElement.ItemHeight = 28;
            radMaskedEditBox_KyHan.Value = 5;
        }

        private bool UpdHopDong(Guid maHopDong, DateTime ngayThanhLy, byte dangThanhLy, decimal phiSauThanhLy, string ghiChu, byte kyHan)
        {
            try
            {
                using (var db = new QuanLyKTXDataContext())
                {
                    var result = (from p in db.tbl_HopDongs
                                  where p.MaHopDong == maHopDong
                                  select p).Single();

                    result.NgayThanhLyHD = ngayThanhLy;
                    result.DangThanhLy = dangThanhLy;
                    result.PhiSauThanhLy = phiSauThanhLy;
                    result.GhiChu = ghiChu;

                    int id = result.ID;                    

                    // Cập nhật trạng thái Sinh viên
                    // Nếu ra khỏi KTX
                    if (dangThanhLy == 1)
                    {
                        var resultSV = (from p in db.tbl_SinhViens
                                        where p.ID == id
                                        select p).FirstOrDefault();
                        resultSV.KhoaTaiKhoan = true;

                        // Cập nhật Xếp phòng
                        var resultXepPhong = (from p in db.tbl_XepPhongs
                                              where p.ID == id && p.NgayRa == null
                                              select p).FirstOrDefault();
                        if (resultXepPhong != null)
                        {

                            db.tbl_XepPhongs.DeleteOnSubmit(resultXepPhong);

                            string soPhong = resultXepPhong.SoPhong;

                            // Cập nhật Giường trống
                            var resultPhong = (from p in db.tbl_Phongs
                                               where p.SoPhong == soPhong
                                               select p).Single();
                            int soGiuongTrong = Convert.ToInt32(resultPhong.SoGiuongTrong) + 1;
                            resultPhong.SoGiuongTrong = Convert.ToByte(soGiuongTrong);

                            // Xóa Hóa đơn phòng
                            var resultHoaDonPhong = from p in db.tbl_HoaDonPhongs
                                                    where p.MaHopDong == maHopDong && p.DaThanhToan == false
                                                    select p;
                            if (resultHoaDonPhong.Count() > 0)
                            {
                                db.tbl_HoaDonPhongs.DeleteAllOnSubmit(resultHoaDonPhong);
                            }
                        }
                    }
                    else // Ký lại hợp đồng
                    {
                        if (dangThanhLy == 0)
                        {
                            int soHopDong = 0;
                            var results = from p in db.tbl_HopDongs
                                          select p;

                            if (results.Count() > 0)
                            {
                                soHopDong = results.Max(p => p.SoHopDong);
                            }

                            tbl_HopDong hopDongMoi = new tbl_HopDong
                            {
                                MaHopDong = Guid.NewGuid(),
                                SoHopDong = soHopDong + 1,
                                HauTo = "VL/KTX/" + ngayThanhLy.Year,
                                ID = id,
                                NgayBatDauHD = ngayThanhLy,
                                NgayKetThucHD = ngayThanhLy.AddMonths(kyHan),
                                LoaiHopDong = result.LoaiHopDong,
                                PhiBanDau = result.PhiBanDau,
                                PhiBanDauVL = result.PhiBanDauVL,
                                TienPhong = result.TienPhong,
                                TienPhongVL = result.TienPhongVL,
                                MienGiam = result.MienGiam,
                                KyHan = kyHan,
                                MaHopDongTruoc = result.MaHopDong
                            };

                            db.tbl_HopDongs.InsertOnSubmit(hopDongMoi);
                        }
                    }
                    db.SubmitChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }
        #endregion

        #region Event

        private void radDropDownList_DangThanhLy_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            if (radDropDownList_DangThanhLy.SelectedIndex.ToString().Equals("0"))
            {
                radMaskedEditBox_KyHan.Enabled = true;
            }
            else
            {
                radMaskedEditBox_KyHan.Enabled = false;
            }
        }

        private void radButton_Lưu_Click(object sender, EventArgs e)
        {
            DateTime ngayThanhLy = radDateTimePicker_NgayThanhLy.Value;
            byte dangThanhLy = Convert.ToByte(radDropDownList_DangThanhLy.SelectedIndex);
            decimal phiThanhLy = Convert.ToDecimal(radMaskedEditBox_PhiSauThanhLy.Value);
            string ghiChu = radTextBox_GhiChu.Text;
            byte kyHan = Convert.ToByte(radMaskedEditBox_KyHan.Value);
            bool flag = UpdHopDong(MaHopDong, ngayThanhLy, dangThanhLy, phiThanhLy, ghiChu, kyHan);
            if (flag) Close();
        }

        private void radButton_Thoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void frm_EndContract_Load(object sender, EventArgs e)
        {
            DefaultLoadForm();
        }

        public frm_EndContract(Guid maHopDong)
        {
            InitializeComponent();
            MaHopDong = maHopDong;
        }

        #endregion
    }
}
